package com.yifushidai.service.impl;

import com.github.pagehelper.PageHelper;
import com.yifushidai.entity.SfzUpload;
import com.yifushidai.mapper.SfzUploadMapper;
import com.yifushidai.service.SfzUploadService;
import com.yifushidai.utils.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class SfzUploadServiceImpl extends BaseService<SfzUpload> implements SfzUploadService {

    @Override
    public List<SfzUpload> selectByPage(Page<Object> page) {
        // 创建Example
        Example example = new Example(SfzUpload.class);
        example.setOrderByClause("begin_time ASC");

        // 创建Criteria 添加条件
        if (StringUtils.isNotBlank(page.getKeyword())) {
            Example.Criteria criteria = example.and();
            criteria.orLike("mac", "%" + page.getKeyword() + "%");
            criteria.orLike("xm", "%" + page.getKeyword() + "%");
            criteria.orLike("xb", "%" + page.getKeyword() + "%");
            criteria.orLike("mz", "%" + page.getKeyword() + "%");
            criteria.orLike("cs", "%" + page.getKeyword() + "%");
            criteria.orLike("zz", "%" + page.getKeyword() + "%");
            criteria.orLike("gmsfzhm", "%" + page.getKeyword() + "%");
            criteria.orLike("qfjg", "%" + page.getKeyword() + "%");
            criteria.orLike("yxqsrq", "%" + page.getKeyword() + "%");
            criteria.orLike("yxjzrq", "%" + page.getKeyword() + "%");
            criteria.orLike("beiyong", "%" + page.getKeyword() + "%");
            criteria.orLike("faceImg", "%" + page.getKeyword() + "%");
            criteria.orLike("backImg", "%" + page.getKeyword() + "%");
        }
        if (StringUtils.isNotBlank(page.getMac())) {
            Example.Criteria criteria2 = example.and();
            criteria2.andEqualTo("mac", page.getMac());
        }

        PageHelper.startPage(page.getPage(), page.getRows());
        return selectByExample(example);
    }

    @Override
    public int saveOrUpdate(SfzUpload record) {
        // 创建Example
        Example example = new Example(SfzUpload.class);
        // 创建Criteria 添加条件
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("mac", record.getMac());
        criteria.andEqualTo("gmsfzhm", record.getGmsfzhm());

        List<SfzUpload> db = selectByExample(example);

        if (db.isEmpty()) {//新建
            return saveNotNull(record);
        } else {//编辑
            return updateByExampleSelective(record, example);
        }
    }

    @Override
    public int deleteByExample(Object key) {
        // 创建Example
        Example example = new Example(SfzUpload.class);

        // 创建Criteria 添加条件
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", key);

        return deleteByExample(example);
    }

    @Override
    public int deleteByExpire() {
        // 创建Example
        Example example = new Example(SfzUpload.class);

        // 创建Criteria 添加条件
        Example.Criteria criteria = example.createCriteria();
        criteria.andLessThanOrEqualTo("endTime", new Date());

        return deleteByExample(example);
    }

}